/*!
 *
 * @version        1
 * @since         
 * @author         
 * @documentation   
 * $Id: btmputil.js 11235 2015-07-13 08:03:18Z liukang $
 *
 */

   
	
/**
  将 datatable 的搜索数据，转换为 btmp rest服务的搜索格式
*/
function _appendTableQuery ( _obj,  _aoData ){
 	
	var obj = jQuery.extend({}, _obj);	
	if(_aoData!=undefined && _aoData!=null){
		var _iDisplayStart = _findArrayByName(_aoData,'iDisplayStart');
		var _iDisplayLength = _findArrayByName(_aoData,'iDisplayLength');
		var _sEcho = _findArrayByName(_aoData,'sEcho');
		
		// 找排序 
		var _iSortCol0 = _findArrayByName(_aoData,'iSortCol_0');
		if(_iSortCol0 !=null){
			var _sSortDir0 = _findArrayByName(_aoData,'sSortDir_0');
			var colsort = _findArrayByName(_aoData,'mDataProp_'+_iSortCol0 );
			if( colsort && _sSortDir0 ){
				if( _sSortDir0 == 'desc' ){
					colsort = '-'+ colsort;
				} 
				
				obj.order = colsort; 
			}
			
		}
		
		var sSearch = _findArrayByName(_aoData,'sSearch');
		if(sSearch!=null && sSearch.trim().length>0){
			
		 	obj.search = (sSearch);
		}
		if(_iDisplayStart!=-1)
			obj.begin = _iDisplayStart;
		if(_iDisplayLength!=-1)
			obj.length = _iDisplayLength;
		obj.sEcho = _sEcho;
	}
	 
	
 	 
	return obj;
	
}			 
	

	

/**
 * 处理数据. 将服务器返回的数据，转为为 DATATABLE 期望的类型 
 * @param  {[type]} resp       [服务器的结果，response]
 * @param  {[type]} fnCallback [转换完，回调函数]
 * @return {[type]}            [description]
 */
function transferResponse(resp ,fnCallback ){
 	var _ntab = {} ;
	if( resp.status == 0 ){
		if( resp.result!=null){
		 
			_ntab.iTotalRecords = resp.len ;
			_ntab.iTotalDisplayRecords = resp.total  ; // resp.page * 10 ;
			_ntab.sEcno = resp.echo;
			_array = _toArray(resp.result , 0 , resp.len );
			_ntab.aaData = _array;

			if( resp.facets )
				_ntab.facets = resp.facets;
			
			fnCallback(_ntab);  
		}
	 }
	 else {
		alert("Error :"+ resp.status + "\n"+ resp.message);
		_ntab.iTotalRecords = 0 ;
		_ntab.iTotalDisplayRecords = 0  ; // resp.page * 10 ;
		_ntab.sEcno = resp.echo;
		_ntab.aaData = [];	
		fnCallback(null);  		
	}

}



// 在原来的数组中取一部分数据
function _sliceArray( obj , start , len ){
	if ( jQuery.type( obj ) != 'array'){
		return null;
	}
	
	if( obj.length < len )
		return obj;
	if( obj.length < len + start ) 
		return obj.slice( start , obj.length - start );
	
	var _dst = obj.slice( start, start + len );
	return _dst;

}

 

		
	function _toArray( obj , start , len ){
		
		var _dst = obj.slice( start, start + len);
		return _dst;

	}

function _findArrayByName( _aoData , name){
	for (var i = 0 ; i< _aoData.length ;i++) {
		if(_aoData[i].name == name )
		return _aoData[i].value ;
	}
	return null;
}

	 
	 
 
/**
 * 将 datatable 的搜索数据，转换为 btmp rest服务的搜索格式
 * @param  {[type]} _aoData [description]
 * @return {[type]}         [description]
 */
function filterData( _aoData ){

	var _target = [];
	 
	var _iDisplayStart = _findArrayByName(_aoData,'iDisplayStart');
	var _iDisplayLength = _findArrayByName(_aoData,'iDisplayLength');
	var _sEcho = _findArrayByName(_aoData,'sEcho');
	
	// 找排序 
	var _iSortCol0 = _findArrayByName(_aoData,'iSortCol_0');
	if(_iSortCol0 !=null){
		var _sSortDir0 = _findArrayByName(_aoData,'sSortDir_0');
		var colsort = _findArrayByName(_aoData,'mDataProp_'+_iSortCol0 );
		if( colsort && _sSortDir0 ){
			if( _sSortDir0 == 'desc' ){
				colsort = '-'+ colsort;
			} 
			
			_target.push( { "name": "order", "value": colsort } ); 
		}
		
	}
	
	var sSearch = _findArrayByName(_aoData,'sSearch');
	if(sSearch!=null && sSearch.trim().length>0){
		_target.push( { "name": "search", "value": sSearch } ); 
	}

	_target.push( { "name": "begin", "value": _iDisplayStart } ); 
	_target.push( { "name": "length", "value": _iDisplayLength } ); 
	_target.push( { "name": "sEcho", "value": _sEcho } );  
	 
	return _target;
	
}
	
	
/**
  获取 服务器数据，转换为 jquery datatable 格式
 */
function retrieveData( _url , _filterSearch , aoData ,  fnCallback ) {
	
  
	// 返回 
	var _aoData = filterData(aoData);
	
	if ( _filterSearch && typeof _filterSearch == 'function'){
		_filterSearch(_aoData);
	}
	
		
	// _aoData.push( { "name": "mult", "value": 5} );   
	//aoData.push( { "name": "acctno", "value": $("#curAcctno").val() } );
	
	$.ajax({
		type : "get",
		async : false,
		url : _url,//sAjaxSource
		dataType : "jsonp",
		data : _aoData, //参数传递   
		jsonp : "jsonpCallback",
		success : function(resp) {
				//alert(resp.status);
			// 转换 rest-response data to jquery-datatable;
			transferResponse(resp, 	fnCallback);
		 
			
		}
	});
}
	
	

 



